package com.letoken.platform.app.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.app.service.AppUserAssetsService;
import com.letoken.platform.pub.constant.RedisKeyConstant;
import com.letoken.platform.pub.enums.LetokenTokenTypeEnum;
import com.letoken.platform.pub.enums.TokenTransactionTypeEnum;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.repo.admin.dao.AnnouncementMapper;
import com.letoken.platform.repo.app.dao.AppUserAssetsHistoryMapper;
import com.letoken.platform.repo.app.dao.AppUserWalletMapper;
import com.letoken.platform.repo.app.manager.TokenManager;
import com.letoken.platform.repo.app.redis.AppJsonStringRedis;
import com.letoken.platform.repo.app.req.token.QueryTokenAssetsHistoryReq;
import com.letoken.platform.repo.app.res.assets.QueryAnnDetailRes;
import com.letoken.platform.repo.app.res.assets.QueryAnnRes;
import com.letoken.platform.repo.app.res.assets.QueryTokenAssetsHistoryRes;
import com.letoken.platform.repo.app.res.assets.QueryTokenAssetsRes;
import com.letoken.platform.repo.app.res.settings.QueryAppNodeAddressListRes;
import com.letoken.platform.repo.utlis.PageDataUtil;
import com.letoken.platform.repo.utlis.WebUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author letokenlabs
 * @date 2022/11/24$
 */
@Service
@Log4j2
public class AppUserAssetsServiceImpl implements AppUserAssetsService {


    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private AnnouncementMapper announcementMapper;

    @Autowired
    private AppUserAssetsHistoryMapper historyMapper;

    @Autowired
    private AppUserWalletMapper appUserWalletMapper;


    /**
     * 查询用户letoken钱包资产
     *
     * @return
     */
    @Override
    public List<QueryTokenAssetsRes> queryTokenAssets() {
        Integer userId = WebUtils.getUserId();
        List<QueryTokenAssetsRes> res = tokenManager.queryTokenAssetsByUser(userId);
        for (int i = 0; i < res.size(); i++) {
            QueryTokenAssetsRes tokenAssets = res.get(i);
            if (tokenAssets.getTokenId().equals(LetokenTokenTypeEnum.USDT.getId())){
                tokenAssets.setType("erc20");
            }else {
                tokenAssets.setType("platform");
            }
        }
        return res;
    }

    /**
     * 系统公告列表
     *
     * @return
     */
    @Cacheable(cacheNames = RedisKeyConstant.ANNOUNCEMENT_LIST, key = "'language_' + #language", sync = true)
    @Override
    public List<QueryAnnRes> queryAssetsList(String language) {
        return announcementMapper.queryAssetsList(language);
    }


    /**
     * 系统公告详情
     *
     * @return
     */
    @Cacheable(cacheNames = RedisKeyConstant.ANNOUNCEMENT_DETAIL, key = "'detail_' + #id", sync = true)
    @Override
    public QueryAnnDetailRes queryAssetsDetail(Integer id) {
        return announcementMapper.queryAssetsDetail(id);
    }

    /**
     * 代币详情-交易记录
     *
     * @return
     */
    public PageData<QueryTokenAssetsHistoryRes> queryTokenAssetsHistory(QueryTokenAssetsHistoryReq req) {
        String type = req.getType();
        List<String> split = null;
        if (type != null) {
            if (!StrUtil.equals(type, "") && !StrUtil.equals(type, "0")) {
                split = StrUtil.split(type, ",");
            }
        }
        Integer userId = WebUtils.getUserId();
        Page<QueryTokenAssetsHistoryRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryTokenAssetsHistoryRes> res = historyMapper.queryTokenAssetsHistory(page, req.getTokenId(), split, userId);
        if(null != res && !CollectionUtils.isEmpty(res.getRecords())){
            res.getRecords().forEach(r-> r.setTypeDesc(TokenTransactionTypeEnum.desc.get(r.getType())));
        }
        return PageDataUtil.getPageData(res);
    }

    @Override
    public BigDecimal queryReleasedBalance() {
        Integer userId = WebUtils.getUserId();
        return appUserWalletMapper.queryReleasedBalance(userId);
    }
}
